package com.ali.gts.controller;

import com.ali.gts.domain.Emp;
import com.ali.gts.exception.GlobalException;
import com.ali.gts.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
public class EmpController {

    Logger logger = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;


    @RequestMapping("/getEmpAndDept")
    @ResponseBody
    public Emp getEmpAndDept(int eid){
        logger.info("参数是{}",eid);
        return empService.getEmpAndDep(eid);
    }


    @RequestMapping("/insertEmp")
    public ModelAndView  insertEmp(@RequestParam Map<String, Object> params) throws GlobalException{
        if(23 == Integer.parseInt((String) params.get("age"))){
            throw new GlobalException("年龄不正确 需要写 23");
        }
        ModelAndView modelAndView = new ModelAndView();
        logger.info("要插入的信息是{}",params);
        empService.insertEmp(params);
        modelAndView.addObject("msg",params.get("empName"));
        modelAndView.setViewName("result");
        return modelAndView;
    }




}
